package de.revdev.llt.module.useradministrtaion;

import de.revdev.llt.module.useradministrtaion.utility.Password;
import de.revdev.llt.module.utility.MessageProvider;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJBException;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.event.ActionEvent;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.event.TabChangeEvent;

/**
 *
 * @author f.lindermaier
 */
@Named(value="persDataBean")
@SessionScoped
public class PersDataBean implements Serializable {

    private User newUserData;
    private Address newAddressData;
    private Credential newCredentialData;
    private UserFacade uf;
    private AddressFacade af;
    private CredentialFacade cf;
    private UserBean userBean;
    private MessageProvider provider;
    private UIComponent emailField;
    private UIComponent oldPwField;
    private String oldPassword;
    private int tabIndex;
    private static final Logger log = Logger.getLogger(PersDataBean.class.getName());

    public PersDataBean() {
    }

    @PostConstruct
    private void init() {
        newAddressData = new Address();
        newCredentialData = new Credential();
        newUserData = new User();
        tabIndex = 0;
        log.log(Level.INFO, "BeanCreated");
    }

    public void onTabChange(TabChangeEvent event) {
        String str = event.getTab().getClientId();
        switch (str) {
            case "tv_pd:t_o":
                newCredentialData = new Credential();
                newUserData = new User();
                newAddressData = new Address();
                tabIndex = 0;
                break;
            case "tv_pd:t_epd":
                newCredentialData = new Credential();
                newUserData = userBean.getUser();
                if (newUserData.getAddress() != null) {
                    newAddressData = newUserData.getAddress();
                } else {
                    newAddressData = new Address();
                }
                tabIndex = 1;
                break;
            case "tv_pd:t_mail":
                newCredentialData = new Credential();
                newUserData = userBean.getUser();
                tabIndex = 2;
                break;
            case "tv_pd:t_pw":
                newCredentialData = userBean.getUser().getCredential();
                tabIndex = 3;
                break;
        }
    }

    public void cancel(ActionEvent event) {
        newCredentialData = new Credential();
        newAddressData = new Address();
        newUserData = new User();
        tabIndex = 0;
    }

    public void saveUserData(ActionEvent event) {
        userBean.setUser(newUserData);
        if (newAddressData.getId() == null) {
            af.create(newAddressData);
        }
        userBean.getUser().setAddress(newAddressData);
        uf.edit(userBean.getUser());
        tabIndex = 0;
    }

    public void saveEmail(ActionEvent event) {
        try {
            uf.findByEmail(newUserData.getEmail());
            provider.addMessage(emailField.getClientId(), FacesMessage.SEVERITY_ERROR, "");
        } catch (EJBException ee) {
            userBean.getUser().setEmail(newUserData.getEmail());
        }
        tabIndex = 0;
    }

    public void savePassword(ActionEvent event) {
        Password pw = new Password();
        String oldPWHash = pw.hashWithoutSalt(oldPassword);
        if (newCredentialData.getPassword().equals(oldPWHash)) {
            userBean.getUser().setCredential(newCredentialData);
            uf.edit(userBean.getUser());
        } else {
            provider.addMessage(oldPwField.getClientId(), FacesMessage.SEVERITY_ERROR, "");
        }
        tabIndex = 0;
        newCredentialData = new Credential();
    }

    @Inject
    public void setUserFacade(UserFacade userFacade) {
        this.uf = userFacade;
    }

    @Inject
    public void setUserBean(UserBean userBean) {
        this.userBean = userBean;
    }

    @Inject
    public void setMessageProvider(MessageProvider provider) {
        this.provider = provider;
    }

    @Inject
    public void setAddressFacade(AddressFacade addressFacade) {
        this.af = addressFacade;
    }

    @Inject
    public void setCredentialFacade(CredentialFacade credentialFacade) {
        this.cf = credentialFacade;
    }

    public User getNewUserData() {
        return newUserData;
    }

    public void setNewUserData(User newUserData) {
        this.newUserData = newUserData;
    }

    public Address getNewAddressData() {
        return newAddressData;
    }

    public void setNewAddressData(Address newAddressData) {
        this.newAddressData = newAddressData;
    }

    public Credential getNewCredentialData() {
        return newCredentialData;
    }

    public void setNewCredentialData(Credential newCredentialData) {
        this.newCredentialData = newCredentialData;
    }

    public UIComponent getEmailField() {
        return emailField;
    }

    public void setEmailField(UIComponent emailField) {
        this.emailField = emailField;
    }

    public UIComponent getOldPwField() {
        return oldPwField;
    }

    public void setOldPwField(UIComponent oldPwField) {
        this.oldPwField = oldPwField;
    }

    public String getOldPassword() {
        return oldPassword;
    }

    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }

    public int getTabIndex() {
        return tabIndex;
    }

    public void setTabIndex(int tabIndex) {
        this.tabIndex = tabIndex;
    }
}
